package com.pb.metadata.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.pb.infra.oth.entity.common.Page;
import com.pb.infra.oth.entity.common.Result;
import com.pb.infra.oth.util.JwtTokenConfig;
import com.pb.metadata.mapper.DimensionAttrManagementMapper;
import com.pb.metadata.mapper.DimensionNoManagementMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 维度编号管理
 */
@Service
public class DimensionNoManagementService {
    /**
     * DAO映射对象
     */
    @Resource
    private DimensionNoManagementMapper dimensionNoManagementMapper;

    /**
     * jwttoken配置类
     */
    @Resource
    private JwtTokenConfig jwtTokenConfig;

    /**
     * 查询分页数据
     * @param map 查询参数
     * @param page 页码
     * @return
     */
    public Result select(Map<String, Object> map, Page page) {
        if (page != null && page.getPageIndex() != null && page.getPageSize() != null) {
            PageHelper.startPage(page.getPageIndex(), page.getPageSize());
        }
        List<Map<String, Object>> list = dimensionNoManagementMapper.select(map);
        return Result.ok(new PageInfo(list).getTotal(), list);
    }

    //新增维度编号
    public Result addDimNoData(Map<String, Object> paramMap) {
        Long dimNo = dimensionNoManagementMapper.selMaxDinNo();
        if (dimNo != null) {
            paramMap.put("dimNo",dimNo + 1);
        } else {
            paramMap.put("dimNo",1);
        }
        dimensionNoManagementMapper.addDimNoData(paramMap);
        return Result.OK_EMPTY;
    }

    @Transactional
    public Result delDimNoData(Map<String, Object> paramMap) {
        String sysFlag = dimensionNoManagementMapper.selectDimNoSysFlag(paramMap);
        if(sysFlag.equals("1")){
            return Result.error("500","该维度类型为系统内置类型，无法删除");
        } else {
            dimensionNoManagementMapper.delDimNoData(paramMap);
            dimensionNoManagementMapper.delComAttrRegDataByDimNo(paramMap);
            dimensionNoManagementMapper.delComAttrRegValueByDimNo(paramMap);
            return Result.OK_EMPTY;
        }

    }

    public Result updateDimNoData(Map<String, Object> paramMap) {
        dimensionNoManagementMapper.updateDimNoData(paramMap);
        return Result.ok("修改成功");
    }

    public Result getComDimAttrRegDataByDimNo(Map<String, Object> map) {
        List<Map<String, Object>> list = dimensionNoManagementMapper.getComDimAttrRegDataByDimNo(map);
        //如果已关联没有选择码值，就取消关联
        for (Map<String, Object> map1 : list) {
            if(map1.get("relaStdCode") == null){
                dimensionNoManagementMapper.updateComRelaCdFlag(map1);
            }
        }
        list = dimensionNoManagementMapper.getComDimAttrRegDataByDimNo(map);
        return Result.ok(list);
    }

    public Result updateComDimAttrRegData(Map<String, Object> paramMap) {
        if(paramMap.get("type") != null && paramMap.get("type").equals("changeRelaStdCode")){
            dimensionNoManagementMapper.updateComDimAttrValue(paramMap);
        }
        dimensionNoManagementMapper.updateComDimAttrRegData(paramMap);
        return Result.OK_EMPTY;
    }

    public Result getComRelaStdCodeList(Map<String, Object> map) {
        List<Map<String, Object>> list = dimensionNoManagementMapper.getComRelaStdCodeList(map);
        return Result.ok(list);
    }

    public Result delComAttrRegData(List<Map<String, Object>> attrRegList) {
        for (Map<String, Object> map : attrRegList) {
            dimensionNoManagementMapper.delComAttrRegData(map);
            dimensionNoManagementMapper.delComAttrRegValue(map);
        }
        return Result.OK_EMPTY;
    }

    public Result addComDimAttrRegData(Map<String, Object> paramMap) {
        dimensionNoManagementMapper.addComDimAttrRegData(paramMap);
        return Result.OK_EMPTY;
    }

    public Result validUnique(Map<String, Object> paramMap) {
        Map map = dimensionNoManagementMapper.validUnique(paramMap);
        if (map == null) {
            return Result.ok(true);
        } else {
            return Result.ok(false);
        }
    }

    @Transactional
    public Result updateUnifyFlag(Map<String, Object> paramMap) {
        dimensionNoManagementMapper.updateUnifyFlag(paramMap);
        //更改非管理法人维度类型的删除状态为1
        System.out.println(paramMap);
        dimensionNoManagementMapper.updateDimTypeDeleteFlag(paramMap);
        return Result.OK_EMPTY;
    }
}
